﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.Template
{
    public partial class AddTemplateIngS : Form
    {
        #region 变量声明
        Tb_User LoginUser;
        NursingFormRecord record = new NursingFormRecord();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo Patientinfo;
        string Id = string.Empty;
        #endregion

        #region 窗体构造函数
        public AddTemplateIngS(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            //构造参数赋值
            this.LoginUser = loginUser;
            this.Patientinfo = patientInfo;
            this.Id = id;
            //绑定数据
            BindInfo(Id);
        }
        #endregion

        #region 确认
        private void btn_QueRen_Click(object sender, EventArgs e)
        {
            Templates ts = new Templates();
            ts.BiaoTi = text_BiaoTi.Text;    //标题
            ts.NeiRong = memo_Text.Text;     //内容

            ts.FormId = (int)FormType.TemplateForm;   //表单Id
            ts.FormName = EnumHelper.GetDescription(FormType.TemplateForm);   //表单名字
            record.FormId = (int)FormType.TemplateForm;   //表单Id
            record.FormName = EnumHelper.GetDescription(FormType.TemplateForm);   //表单名字
            record.PatientId = Patientinfo.PATIENT_ID;  //住院号
            record.VisitId = Patientinfo.VISIT_ID;   //住院次数
            record.PatientName = Patientinfo.PATIENT_NAME;   //患者姓名
            record.BedNo = Patientinfo.BED_NO;   //床号
            record.DepartmentId = Patientinfo.DEPARTMENT_ID;   //当前科室ID
            record.DepartmentName = Patientinfo.DEPARTMENT_NAME;   //当前科室名字
            record.AreaId = Patientinfo.NURSE_CELL_CODE;   //当前患者病区ID
            record.AreaName = Patientinfo.NURSE_CELL_NAME;   //当前患者病区名称

            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            record.NursingFormData = XmlUtil.ObjectToXML(ts, Encoding.UTF8);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }

        }
        #endregion

        #region 取消
        private void btn_QuXiao_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
        #endregion

        #region 显示赋值
        public void BindInfo(string Id)
        {
            if (string.IsNullOrEmpty(Id)) return;
            string sql = string.Format("SELECT NursingFormData FROM NursingFormRecord WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            Templates tls = new Templates();
            tls = XmlUtil.XMLToObject<Templates>(NursingFormData, Encoding.UTF8);
            text_BiaoTi.Text = tls.BiaoTi;    //标题
            memo_Text.Text = tls.NeiRong;     //内容

        }
        #endregion


    }
}
